package com.lemon.lemonai.tools;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.lemon.lemonai.entity.po.Course;
import com.lemon.lemonai.entity.po.CourseReservation;
import com.lemon.lemonai.entity.po.School;
import com.lemon.lemonai.entity.query.CourseQuery;
import com.lemon.lemonai.service.ICourseReservationService;
import com.lemon.lemonai.service.ICourseService;
import com.lemon.lemonai.service.ISchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 定义工具（Function）
 * @Author: lemon
 * @CreateTime: 2025-03-24  15:35
 * @Version: 1.0
 */
@Component
@RequiredArgsConstructor
public class CourseTools {

    private final ICourseService courseService;

    private final ISchoolService schoolService;

    private final ICourseReservationService reservationService;

    @Tool(description = "根据条件查询课程信息")
    public List<Course> queryLessons(@ToolParam(description = "课程查询条件", required = false) CourseQuery courseQuery) {
        if (courseQuery == null) {
            return courseService.list();
        }
        QueryChainWrapper<Course> wrapper = courseService.query()
                .eq(courseQuery.getType() != null, "type", courseQuery.getType())
                .le(courseQuery.getEdu() != null, "edu", courseQuery.getEdu());
        if (courseQuery.getSorts() != null && !courseQuery.getSorts().isEmpty()) {
            for (CourseQuery.Sort sort : courseQuery.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }

    @Tool(description = "查询所有校区")
    public List<School> querySchools() {
        return schoolService.list();
    }


    @Tool(description = "生成课程预约单，并返回生成的预约单号")
    public Integer saveReservationOrder(
            @ToolParam(description = "预约课程") String course,
            @ToolParam(description = "预约校区") String school,
            @ToolParam(description = "学生姓名") String studentName,
            @ToolParam(description = "联系方式") String contactInfo,
            @ToolParam(description = "备注", required = false)String remark) {
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(course);
        courseReservation.setSchool(school);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setRemark(remark);
        reservationService.save(courseReservation);
        return courseReservation.getId();
    }
}
